Ext.onReady(function() {
	var toolbar = Ext.create('Ext.toolbar.Toolbar', {
		dock : 'top',
		items : [{
					text : '返回',
					xtype : 'button',
					listeners : {
						click : function() {
							location.href = "sysMgr/message_sys";
						}
					}
				}, {
					text : '添加',
					xtype : 'button',
					listeners : {
						click : function() {
							var form = Ext.getCmp("add_form");
							if (!form.isValid()) {
								return;
							}
							var grid = Ext.getCmp("user_grid");
							var store = grid.getStore();
							var models = store.getRange(0, store.getCount());
							var userIds = [];
							for(var i=0; i<models.length;i++){
								userIds[i] = models[i].get('Id');
							}
							var params = {
								DepartmentId : Ext.getCmp("Department").getValue(),
								UserIds : userIds,
								NotifyTime : Ext.Date.format(Ext.getCmp("NotifyTime").getValue(), 'Y-m-d H:i:s'),
								NotifyContent : Ext.getCmp("NotifyContent").getValue(),
								NotifyTimes : Ext.getCmp("NotifyTimes").getValue()
							}
							Ext.Ajax.request({
								url : '/sms_task/add',
								type : 'POST',
								params : params,
								callback : function(options, success, response) {
									if (success) {
										var result = Ext
												.decode(response.responseText);
										if (result.success) {
											location.href = "/sysMgr/message_sys";
										} else {
											Ext.Msg.alert("错误", result.msg);
										}
									}
								}
							})
						}
					}
				}]
	});

	var departmentStore = Ext.create('Ext.data.Store', {
				fields : [{
							name : 'Id',
							type : 'int'
						}, 'Name'],
				proxy : {
					type : 'ajax',
					url : '/department/all',
					reader : {
						type : 'json',
						root : 'data.rows'
					}
				},
				autoLoad : true
			});
	var department = Ext.create('Ext.form.ComboBox', {
				fieldLabel : '部门',
				margin : '0 5',
				allowBlank : false,
				editable : false,
				labelWidth : 60,
				width : 200,
				id : 'Department',
				store : departmentStore,
				queryMode : 'local',
				displayField : 'Name',
				valueField : 'Id',
				listeners : {
					afterRender : function(combo) {
						combo.setValue(1);
					},
					change : function(self, newValue, oldValue) {
						Ext.Ajax.request({
									url : "user/find_by_dept/" + newValue,
									callback : function(options, success,
											response) {
										if (success) {
											user.setValue(null);
											var result = Ext.decode(response.responseText);
											if (result.success) {
												userStore.removeAll();
												userStore.loadData(result.data.rows);
												if (!Ext.isEmpty(result.data.rows)) {
													user.setValue(result.data.rows[0].Id);
												}
											}
										}
									}
								});
					}
				}
			});

	var userStore = Ext.create('Ext.data.Store', {
				fields : ["Id", "Username", 'Name']
			});
	var user = Ext.create("Ext.form.ComboBox", {
				width : 400,
				id : 'User',
				store : userStore,
				editable : false,
				multiSelect : true,
				allowBlank : false,
				queryMode : 'local',
				valueField : 'Id',
				tpl : Ext.create(
								'Ext.XTemplate',
								'<tpl for=".">',
								'<div class="x-boundlist-item">{Username}({Name})</div>',
								'</tpl>'),
				displayTpl : Ext.create('Ext.XTemplate', '<tpl for=".">',
						'{Username}({Name}) ', '</tpl>')
			});

	var timesStore = Ext.create('Ext.data.Store', {
				fields : ['display', 'value'],
				data : [{
							"display" : "立刻",
							"value" : "0"
						}, {
							"display" : "1小时",
							"value" : "1"
						}, {
							"display" : "2小时",
							"value" : "2"
						}]
			});

	var times = Ext.create('Ext.form.ComboBox', {
				fieldLabel : '提醒周期',
				id : 'NotifyTimes',
				labelWidth : 60,
				editable : false,
				width : 250,
				margin : '5 10',
				store : timesStore,
				queryMode : 'local',
				displayField : 'display',
				valueField : 'value',
				listeners : {
					afterRender : function(combo) {
						var firstValue = combo.getStore().first().get("value");
						combo.setValue(firstValue);
					}
				}
			});

	var store = Ext.create('Ext.data.Store', {
				fields : ["Id", "Username", 'Name']
			});
	var userGrid = Ext.create('Ext.grid.Panel', {
				id : "user_grid",
				minHeight: 60,
				width : '90%',
				selType : 'checkboxmodel',
				store : store,
				columns : [{
							text : '序号',
							xtype : 'rownumberer',
							width : 40,
							sortable : false
						}, {
							text : '用户',
							flex : 1,
							renderer : function(v, meta, record) {
								return record.get('Username') + '('
										+ record.get('Name') + ')';
							}
						}, {
							text : '操作',
							xtype : 'actioncolumn',
							width : 50,
							items : [{
										icon : 'view/images/delete.png',
										tooltip : 'Delete',
										handler : function(grid, rowIndex,
												colIndex) {
											grid.getStore().removeAt(rowIndex);
										}
									}]
						}]
			});

	var panel = Ext.create("Ext.form.Panel", {
				id : "add_form",
				border : false,
				autoScroll: true,
				bodyBorder : false,
				dockedItems : [toolbar],
				items : [{
					xtype : "container",
					layout : "hbox",
					minWidth: 700,
					margin: '5 5',
					items : [department, user, {
						xtype : 'button',
						margin : '0 5',
						text : '添加',
						listeners : {
							click : function() {
								var user = Ext.getCmp("User");
								var userGridStore = Ext.getCmp("user_grid").getStore();
								var userArr = user.getValue();
								for(var i=0; i<userArr.length; i++){
									var record = user.findRecordByValue(userArr[i]);
									if(record && userGridStore.findRecord('Id', record.get('Id')) == null){
										userGridStore.add(record);
									}
								}
								
							}
						}
					}]
				}, {
					xtype : 'container',
					margin : '5 10',
					minWidth: 700,
					items : [userGrid]
				}, {
					xtype : 'textareafield',
					maxLength : 70,
					allowBlank : false,
					id : "NotifyContent",
					width : '90%',
					margin : '5 10'
				}, {
					xtype : "container",
					layout : "hbox",
					items : [{
								xtype : 'datefield',
								id : "NotifyTime",
								labelWidth : 60,
								width : 250,
								margin : '5 10',
								fieldLabel : '提醒时间',
								format : 'Y-m-d H:i:s',
								value : new Date()
							}, times]
				}]
			});
	Ext.create('Ext.Viewport', {
				layout : 'fit',
				style : 'background:none;background-color:white;',
				items : [panel]
			});
});